package cn.cs.common;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class JPushUtil {
    protected static final Logger LOG = LoggerFactory.getLogger(JPushUtil.class);
    private static final String appKey = "ed7048ae9f80a0fd8c577038";
    private static final String masterSecret = "07943182d88c60a9996a1685";

    private static final String appKeyJXS = "3eb8fce22a3b002ddaed34b8";
    private static final String masterSecretJXS = "c39a656d80aec11614990e6d";
    public static final String ALERT = "测试内容2019年4月2日18:21:27";

    public static JPushClient jpushClient=null;

    public static JPushClient jpushClientJXS=null;

    int sendNo = 1;
    static String msgTitle = "测试标题";
    static String msgContent = "测试内容";

    public static Object testSendPush(String[] alias, String msgContent, Map<String, String> map) {
        jpushClient = new JPushClient(masterSecret, appKey);

        PushPayload payload = buildPushObject_alias_alert(alias, msgContent, map);
//    	PushPayload payload = buildPushObject_alias_alert_IOS(alias, msgContent, map);
        PushResult result = null;
        try {
            System.out.println(payload.toString());
            result = jpushClient.sendPush(payload);

        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);

        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
        return result;
    }

    public static Object testSendPushJXS(String[] alias, String msgContent, Map<String, String> map) {
        jpushClientJXS = new JPushClient(masterSecretJXS, appKeyJXS);

        PushPayload payload = buildPushObject_alias_alert(alias, msgContent, map);
        PushResult result = null;
        try {
            System.out.println(payload.toString());
            result = jpushClientJXS.sendPush(payload);
        } catch (APIConnectionException e) {
            LOG.error("Connection error. Should retry later. ", e);
        } catch (APIRequestException e) {
            LOG.error("Error response from JPush server. Should review and fix it. ", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
            LOG.info("Msg ID: " + e.getMsgId());
        }
        return result;
    }

    public static PushPayload buildPushObject_all_alias_alert(String msgContent, Map<String, String> map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(msgContent)
                                .setBadge(+1)
                                .addExtra("from", "JPush")
                                .addExtras(map)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .build())
                .build();
    }
    public static PushPayload buildPushObject_alias_alert(String[] alias, String msgContent, Map<String, String> map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.alias(alias)) //指定某个用户接收
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(msgContent)
                                .setBadge(+1)
                                .setSound("default")
                                .setMutableContent(true)
                                .addExtra("from", "JPush")
                                .addExtras(map)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setAlert(msgContent)
                                .addExtra("from", "JPush")
                                .addExtras(map)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(false)
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_alias_alert_IOS(String[] alias, String msgContent, Map<String, String> map) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.alias(alias)) //指定某个用户接收
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(msgContent)
                                .setBadge(+1)
                                .setSound("default")
                                .addExtra("from", "JPush")
                                .addExtras(map)
                                .build())
                        .build())
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }
    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(ALERT, msgTitle, null))
                .build();
    }

    public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.newBuilder()
                        .setAlert("alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title").build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(+1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(+1)
                                .setSound("default")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(msgContent))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                        .addAudienceTarget(AudienceTarget.alias("user_26c4d29029734b469cdbbed9e04c6ac4", "alias2"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(msgContent)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }

    public static void main(String[] args) {
        String[] strSelect = new String[1];
        strSelect[0] = "643512692778209280";
        Map<String, String> cmap = new HashMap<String, String>();
        cmap.put("subscribeId", "9a5e017a4d5aa2c22c30fcd3");
        Object obj = testSendPushJXS(strSelect, "您有新的预约订单，请查看", cmap);
        System.out.println(obj);
    }

}
